package com.woniuxy.util;

import java.util.*;

public class FindFriends {
    public static HashSet<Long> findAllFriends2(Map<Long,List<Long>> all) {
        Set<Long> result = new HashSet<Long>();

        Set<Long> longs = all.keySet();
        List<Long> keys = new ArrayList<>(longs);

        Collection<List<Long>> values = all.values();

        for (int i = 0; i < keys.size()-1; i++) {
            Set<Long> fans = new HashSet<>(all.get(keys.get(i)));
            Set<Long> fans2 = new HashSet<>(all.get(keys.get(i+1)));
            fans.retainAll(fans2);
            result.addAll(fans);
        }

        return (HashSet<Long>) result;
    }
}
